package good_src.Strategy;

import good_src.Implementation.Canal;
import good_src.Implementation.CapteurProxy;
import good_src.Interfaces.Algorithme;
import good_src.Interfaces.Capteur;
import good_src.Interfaces.ObservateurCapteur;

/**
 * 
 * @author Tangi Midy & Fabien Le Peih
 * 
 * <p>Sequential Strategy is the simple Strategy : when the capteur ticks, every requester is updated</p>
 * <p>The values could be stored in a stack and the capteur could tick without waiting for teh requesters update
 * But we decided to apply directly in the same Thread the update. Consequently, the capteur waits before ticking.</p>
 *<p> Otherwise we use here a CapteurProxy which is a Capteur that contains the value to set to the requesters. With this
 *object, we are sure to update the correct value</p>
 */
public class Sequentielle implements Algorithme {

	private Capteur<Integer> capteurInitial;
	private CapteurProxy proxy;

	/**
	 * Constructor of the class
	 * @param capteurInitial The initial capteur
	 */
	public Sequentielle(Capteur<Integer> capteurInitial) {
		this.capteurInitial = capteurInitial;
	}

	/**
	 * 
	 * @see good_src.Interfaces.Algorithme#configure()
	 */
	@Override
	public void configure() {
		proxy = new CapteurProxy();
		proxy.setStrategy(this);
		proxy.setCanaux(this.capteurInitial.getObservers());
		
		for (ObservateurCapteur  c :proxy.getCanaux()){
			((Canal)c).setSubject(proxy);
		}
	}
	
	/**
	 * 
	 * @see good_src.Interfaces.Algorithme#execute()
	 */
	@Override
	public void execute() {

		proxy.setValue(capteurInitial.getValue());
		//We check the proxy and not the inital capteur
		for (ObservateurCapteur  c :proxy.getCanaux()){
			c.update();
		}		
	}

}
